{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Requirements\n",
    "!pip install nibabel vtk pyacvd pyvista simpleitk "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "from k3d.helpers import download, contour\n",
    "from vtk.util import numpy_support\n",
    "import SimpleITK as sitk\n",
    "import vtk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = download('https://vedo.embl.es/examples/data/embryo.slc')\n",
    "reader = vtk.vtkSLCReader()\n",
    "reader.SetFileName(filename)\n",
    "reader.Update()\n",
    "vti = reader.GetOutput()\n",
    "\n",
    "bounds = vti.GetBounds()\n",
    "x, y, z = vti.GetDimensions()\n",
    "volume_data = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seg = sitk.BinaryThreshold(\n",
    "    sitk.GetImageFromArray(volume_data),\n",
    "    lowerThreshold=20,\n",
    "    upperThreshold=255\n",
    ")\n",
    "\n",
    "seg = sitk.BinaryErode(seg, (2, 2, 2))\n",
    "mask = sitk.GetArrayFromImage(seg)\n",
    "\n",
    "def get_bounds(img):\n",
    "    origin = img.GetOrigin()\n",
    "    size = np.array(img.GetSpacing()) * np.array(img.GetDimensions())\n",
    "\n",
    "    return np.array([origin[0], origin[0] + size[0],\n",
    "                   origin[1], origin[1] + size[1],\n",
    "                   origin[2], origin[2] + size[2]])\n",
    "\n",
    "mesh = k3d.vtk_poly_data(contour(mask, get_bounds(vti), [1]), color=0xff0000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embryo = k3d.volume_slice(volume_data.astype(np.float16), \n",
    "                          mask=mask,\n",
    "                          active_masks=[1],\n",
    "                          color_map_masks = [0, 0x0000ff],\n",
    "                          color_map=np.array(k3d.paraview_color_maps.Grayscale, dtype=np.float32), \n",
    "                          slice_z=volume_data.shape[0]//2,\n",
    "                          slice_y=volume_data.shape[1]//2,\n",
    "                          slice_x=volume_data.shape[2]//2,\n",
    "                          bounds=bounds)\n",
    "\n",
    "plot = k3d.plot(camera_mode='volume_sides', grid_visible=False, background_color=0)\n",
    "plot += embryo\n",
    "plot += mesh\n",
    "plot.slice_viewer_mask_object_ids = [mesh.id]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot2 = k3d.plot()\n",
    "plot2 += k3d.volume(volume_data.astype(np.float16), bounds=bounds, mask=mask, mask_opacities=[0,1])\n",
    "plot2.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot2.objects[0].mask_opacities = [0.25,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
